var fs = require("fs");
var path = require("path");



module.exports = function (UC, source, config, ArFor, cache, package) {
    var UserControl = UC;
    var router = function (req, res, next) {
        var ajaxTest = {
            error: 0
        };
        var Request = req.body;
        if (Request["sourceid"] != null && UserControl.Source(req.session.key, Request["sourceid"])) {
            Request = UserControl.GetForm(req.sessionID, Request);
            new source(Request["sourceid"], Request, function (db) {
                var ajaxTest = {
                    error: (db != null ? 0 : 1)
                };
                if (Request.AppsInitFunctions) {
                    config.AppsInitFunctions(Request.AppsInitFunctions, function () {
                        ajaxTest.AppsInitFunctions = Request.AppsInitFunctions;
                        res.Json(ajaxTest);
                    });
                } else {
                    res.Json(ajaxTest);
                }
            });

        } else {
            var ajaxTest = {
                error: 2
            };
            res.Json(ajaxTest);
        }
    }
    return router;

}